# -*- coding: utf-8 -*-

#from django.contrib import admin as _admin
from xadmin import admin 
from xadmin.admin import XModelAdmin

#from django.db import models
from django.contrib.admin import TabularInline, StackedInline
from django.contrib.auth import models

from testapp.models import *

from django.contrib.auth.models import User,Message
class MessageAdmin(admin.ModelAdmin):
    pass

from django.contrib.auth.admin import UserAdmin as _ua

class UserAdmin(admin.ModelAdmin, _ua):
    list_display   = ('username', 'first_name', 'last_name', 'email', 'date_joined', 'last_login', 'is_staff', 'is_superuser')
    search_fields  = ['username', 'first_name',  'last_name', 'email']
    list_filter    = ['is_staff', 'is_superuser', 'date_joined', 'last_login']
    date_hierarchy = 'date_joined'
    fk_search_on = ['username',]

class BookAdmin(XModelAdmin):
    list_display   = ('title','_author', 'abstract', 'year')
    list_filter = ['author','year']
    enable_export_csv_url = True
    buttons =['testapp_book_csv']
    xmodel_link = ['author']
    
    _author = XModelAdmin.get_model_change_href('author')

class BookStackedInline(StackedInline):
    model = Book
    verbose_name_plural = "Books (StackedInline)"
    

class ArticleTabularInline(TabularInline):
    model = Article
    verbose_name_plural = "Article (TabularInline)"

class AllFieldsAdmin(XModelAdmin):
    list_display = ('char_field', 'boolean_field','date_field','email_field', 'image_field', 'integer_field','time_field','url_field','fk_field','_text_field')
    list_filter = ['char_field','boolean_field','date_field']
    search_fields = ('char_field', 'slug_field', 'fk_field__username')
    
    def _text_field(self, obj):
        return getattr(obj, 'text_field')[:30]
    _text_field.text_preview = True
    
class AuthorAdmin(XModelAdmin):
    list_display   = ('full_name', 'born','wp_url')
    search_fields  = ['full_name',]
    list_filter = ['full_name',]
    date_hierarchy = 'born'
    inlines = (BookStackedInline, ArticleTabularInline)
    filters_spec = {'full_name': "alphabetic_filter" }
    fk_search_on = ['full_name',]
    
class XAdminExtededFieldsAdmin(XModelAdmin):
    pass



admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.site.register(Message, MessageAdmin)

admin.site.register(Author, AuthorAdmin)
admin.site.register(Book, BookAdmin)
admin.site.register(AllFields, AllFieldsAdmin)
admin.site.register(XAdminExtededFields, XAdminExtededFieldsAdmin)
